﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using MimicAPI.Window;

namespace Mimic
{
    /// <summary>This window contains only the thumbnail control</summary>
    public partial class ThumbnailWindow : Window
    {
        /// <summary>Size of the default windows caption</summary>
        const int DefaultWindowsCaptionSize = 20;
        /// <summary>Size of the borders (sum of both sides)</summary>
        const int DefaultBorderSize = 2 * 8;    // * 2 because we use the sum

        public ThumbnailWindow(Window wnd, IntPtr hWindow)
        {
            this.Owner = wnd;
            InitializeComponent();

            thumbnail.Source = hWindow;
            this.Icon = hWindow.GetWindowBitmapFrame() ?? BitmapFrame.Create(new Uri(@"pack://application:,,/Resources/bullet_black.png"));
        }

        void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Size? srcSize = thumbnail.GetSourceSize();
            if (!srcSize.HasValue)
            {
                // The window does not exist ! (probably it has been closed)
                this.Close();
                return;
            }

            // Manage the size and the start location of the window
            this.Left += (this.Width - srcSize.Value.Width - DefaultBorderSize) / 2;
            if (this.Left < 0)
                this.Left = 0;
            this.Top += (this.Height - srcSize.Value.Height - DefaultWindowsCaptionSize - DefaultBorderSize) / 2;
            if (this.Top < 0)
                this.Top = 0;
            this.Width = srcSize.Value.Width + DefaultBorderSize;
            this.Height = srcSize.Value.Height + DefaultWindowsCaptionSize + DefaultBorderSize;

            // Allow to move the windows
            this.EnableMoving();
        }

        void Window_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)
            {
                this.WindowStyle = WindowStyle.ToolWindow;
                this.WindowState = WindowState.Normal;
            }
            else
            {
                this.WindowStyle = WindowStyle.None;
                this.WindowState = WindowState.Maximized;
            }
        }
    }

}
